package com.util

import io.netty.util.HashedWheelTimer
import io.netty.util.Timeout
import io.netty.util.TimerTask
import java.util.concurrent.TimeUnit

object TimerUtils {

    private val hashedWheelTimer by lazy { HashedWheelTimer() }

    @JvmStatic
    fun newTimeout(task: TimerTask, delay: Long, unit: TimeUnit = TimeUnit.MILLISECONDS): Timeout {
        return hashedWheelTimer.newTimeout(task, delay, unit)
    }

    @JvmStatic
    fun newTimeout(task: (timeout: Timeout) -> Unit, delay: Long, unit: TimeUnit = TimeUnit.MILLISECONDS): Timeout {
        return hashedWheelTimer.newTimeout(task, delay, unit)
    }

    /**
     * 释放此计时器获取的所有资源，并取消所有已计划但尚未执行的任务。
     */
    @JvmStatic
    fun stop() {
        hashedWheelTimer.stop()
    }

}
